﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace VSM.UC
{
    public partial class ucBP : System.Web.UI.UserControl
    {
        private string dpType;

        public string BPType
        {
            get { return dpType; }
            set { dpType = value; }
        }
        private Label lblInfo;

        public Label LblInfo
        {
            get { return this.lblCustomerInfo; }
            set { lblInfo = value; }
        }
        private TextBox txtCode;

        public TextBox TxtCode
        {
            get { return this.txtCustomerCode; }
            set { txtCode = value; }
        }
        private TextBox txtName;

        public TextBox TxtName
        {
            get { return this.txtCustomerName; }
            set { txtName = value; }
        }

        private TextBox txtCurrencyType;

        public TextBox TxtCurrency
        {
            get { return this.txtCurrency; }
            set { txtCurrencyType = value; }
        }

        private Button btnSelect;

        public Button BtnSelect
        {
            get { return this.btnSelectBP; }
            set { btnSelect = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtCustomerCode_TextChanged(object sender, EventArgs e)
        {
            this.txtCustomerName.Text = "";
            this.txtCurrency.Text = "";
            DataSet ds = (DataSet)Session["BPSearch"];
            this.txtCustomerName.Text = string.Empty;
            if (ds == null)
            {
                LoadBP();
                ds = (DataSet)Session["BPSearch"];
            }
            if (ds.Tables.Count > 0)
            {
                DataRow[] r = ds.Tables[0].Select("CardCode='" + this.txtCustomerCode.Text + "'");
                if (r.Length > 0)
                {
                    this.txtCustomerName.Text = r[0]["CardName"].ToString();
                    this.txtCurrency.Text = r[0]["Currency"].ToString();
                }
                else
                {
                    OpenDPPopup(this.BPType);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cardType"></param>
        private void OpenDPPopup(string cardType)
        {
            string popup = "OpenBP('" + cardType + "')";
            ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", popup, true);
        }
        /// <summary>
        /// 
        /// </summary>
        private void LoadBP()
        {
            try
            {
                MasterData obj = new MasterData();
                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                DataSet dsBP = null;

                if (dpType.Equals("S"))
                    dsBP = obj.GetVendors("VSM", Session[Utils.AppConstants.ID_Dealer].ToString());
                else
                    dsBP = obj.GetCustomers(dbType, Session[Utils.AppConstants.ID_Dealer].ToString(), "( 103, 104 )");
                Session["BPSearch"] = dsBP;
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSelectBP_Click(object sender, EventArgs e)
        {
            OpenDPPopup(this.BPType);
        }
    }
}